package com.admin.service.impl;

import cn.easyes.common.utils.StringUtils;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.admin.es.entity.EsSchoolActivityEntity;
import com.admin.es.mapper.EsSchoolActivityMapper;
import com.admin.user.entity.SchoolActivity;
import com.admin.user.mapper.SchoolActivityMapper;
import com.admin.service.SchoolActivityService;
import com.admin.utils.DataTransitionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学校活动管理service实现类
 * @author 武佳敏
 * @date 2023-02-01
 */
@Service
public class SchoolActivityServiceImpl implements SchoolActivityService {

    @Autowired
    SchoolActivityMapper schoolActivityMapper;

    @Autowired
    EsSchoolActivityMapper esSchoolActivityMapper;

    /**
     * 条件查询学校活动列表
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Override
    public List<EsSchoolActivityEntity> getList(SchoolActivity schoolActivity) {
        List<EsSchoolActivityEntity> esUserEntities = esSchoolActivityMapper.selectList(new LambdaEsQueryWrapper<EsSchoolActivityEntity>()
                .like(StringUtils.isNotBlank(schoolActivity.getSchoolName()),EsSchoolActivityEntity::getSchoolName, schoolActivity.getSchoolName())
                .like(StringUtils.isNotBlank(schoolActivity.getParticipant()),EsSchoolActivityEntity::getParticipant, schoolActivity.getParticipant())
                .like(StringUtils.isNotBlank(schoolActivity.getActivityAddress()),EsSchoolActivityEntity::getActivityAddress, schoolActivity.getActivityAddress())
                .like(StringUtils.isNotBlank(schoolActivity.getPrincipal()),EsSchoolActivityEntity::getPrincipal, schoolActivity.getPrincipal())
                .like(StringUtils.isNotBlank(schoolActivity.getPrincipalPhone()),EsSchoolActivityEntity::getPrincipalPhone, schoolActivity.getPrincipalPhone())
                .like(StringUtils.isNotBlank(schoolActivity.getIssueStatus()),EsSchoolActivityEntity::getIssueStatus, schoolActivity.getIssueStatus())
                .like(StringUtils.isNotBlank(schoolActivity.getIssueStatus()),EsSchoolActivityEntity::getActivityName, schoolActivity.getActivityName())
                .eq(schoolActivity.getId()!= null,"id", schoolActivity.getId()));
        return esUserEntities;
    }

    /**
     * 删除es中的数据
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEsData() {
        //删除es中的数据
        esSchoolActivityMapper.delete(new LambdaEsQueryWrapper<>());
    }

    /**
     * 新增学校活动
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SchoolActivity schoolActivity) {
        //保存数据到数据库
        int insert = schoolActivityMapper.insert(schoolActivity);
        if(insert>0){
            //数据库实体转换es实体
            EsSchoolActivityEntity entity = DataTransitionUtils.EntityToEsEntity(schoolActivity);
            //保存数据到es
            esSchoolActivityMapper.insert(entity);
        }
    }

    /**
     * 通过id修改学校活动
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(SchoolActivity schoolActivity) {
        //修改数据库中的数据
        int insert = schoolActivityMapper.updateById(schoolActivity);
        if(insert>0){
            //数据库实体转换es实体
            EsSchoolActivityEntity entity = DataTransitionUtils.EntityToEsEntity(schoolActivity);
            //修改es中的数据
            esSchoolActivityMapper.updateById(entity);
        }
    }

    /**
     * 通过id删除学校活动
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delById(Long id) {
        //根据id删除数据
        int insert = schoolActivityMapper.deleteById(id);
        if(insert>0){
            //删除es中的数据
            esSchoolActivityMapper.deleteById(id);
        }
    }
}
